package thread;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;

public class ThreadDemo35 {
    public static void main(String[] args) throws InterruptedException {
        // 1. 表示有 10  个任务
        CountDownLatch latch = new CountDownLatch(10);

        Random random = new Random();
        long time = (random.nextInt(5) + 1) * 1000;

        // 2. 创建 10 个线程开始执行任务
        for(int i = 0; i < 10; i++){
            int n = i;
            Thread t = new Thread(() -> {
                System.out.println(n + "开始下载");
                try {
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(n + "下载结束");
                latch.countDown();
            });
            t.start();
        }
        // 3. 等待所有任务执行结束
        latch.await();
        System.out.println("全部任务下载结束");
    }
}
